package org.tmad.ocms.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.tmad.ocms.log.LogFactory;
import org.tmad.ocms.model.AjaxOperationType;
import org.tmad.ocms.service.MasterDataService;

/**
 * Servlet implementation class AjaxServlet
 */
public class AjaxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = LogFactory.getLogger();	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AjaxServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AjaxOperationType operation = null;
		response.setContentType("text/json");
        PrintWriter out = response.getWriter();
        
        String method =request.getParameter("method");
        String typeId = request.getParameter("id");
        String responseString = "";
        if(method != null){
        	operation = AjaxOperationType.valueOf(method);
        	switch(operation){
        	
        	case CaseCategory:
        		responseString = getCaseSubCategories(typeId);
        		break;
        	case HelpCategory:
        		responseString = getHelpSubCategories(typeId);
        		break;
        	default:
        		
        	}
        	
        out.write(responseString);
        	
        }
        
        
        try {
        
        }finally{
        	
        }
	}

	private String getHelpSubCategories(String typeId) {
		
			 StringBuffer response = new StringBuffer("{");
			 Integer id = Integer.parseInt(typeId); //TODO make sure invalid string is not given. validate properly
			 Map<Integer, String> subCategories =MasterDataService.getHelpSubCategories(id);
			 Iterator<Integer> ir = subCategories.keySet().iterator();
		        while (ir.hasNext()) {
		        	Integer categoryId = ir.next();
		        	response.append("{id:\"" + categoryId +", name:\"" + subCategories.get(categoryId) +  "\"},");
		        }
		        response.setCharAt( response.length()-1,'}');  // replace last character with ]
		        
			return response.toString() ;
		}

	

	private String getCaseSubCategories(String typeId) {
		 StringBuffer response = new StringBuffer("{");
		 Integer id = Integer.parseInt(typeId); //TODO make sure invalid string is not given. validate properly
		 Map<Integer, String> subCategories =MasterDataService.getCaseSubCategories(id);
		 Iterator<Integer> ir = subCategories.keySet().iterator();
	        while (ir.hasNext()) {
	        	Integer categoryId = ir.next();
	        	response.append("{id:\"" + categoryId +", name:\"" + subCategories.get(categoryId) +  "\"},");
	        }
	        response.setCharAt( response.length()-1,'}');  // replace last character with }	        
		return response.toString() ;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
